package com.ptsolusi.ceriacrudwicket.tool.pages;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;

import com.ptsolusi.ceriacrudwicket.model.Customer;
import com.ptsolusi.ceriacrudwicket.tool.constant.OperationType;
import com.ptsolusi.ceriacrudwicket.tool.dataproviders.CustomerDataProvider;
import com.ptsolusi.ceriacrudwicket.tool.pages.windows.CustomerPanel;
import com.ptsolusi.ceriacrudwicket.tool.pages.windows.SearchCustomerPanel;

public class CustomerList extends BasePage {
	private final int MAX_ITEMS_PER_PAGE = 6;
	
	public CustomerList(final PageParameters params){
		
		add(new Label("subheader", "Customer List"));
		
		
		add(new SearchCustomerPanel("searchPanel", params));
		
		
		final ModalWindow customerWindow = new ModalWindow("customer_window");
		
		final WebMarkupContainer container = new WebMarkupContainer("container");
        container.setOutputMarkupId(true);
        
        CustomerDataProvider provider = null;
        if(params != null && OperationType.SEARCH.toString().equals(params.getString(OperationType.SEARCH.toString()))
        		&& params.getString(SearchCustomerPanel.pFirstName) != null)
        	provider = new CustomerDataProvider(params.getString(SearchCustomerPanel.pFirstName));
        else
        	provider = new CustomerDataProvider();
        
		//list
		DataView dataView = new DataView("pageable", provider){
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item item) {
				final Customer customer = (Customer) item.getModelObject();
				item.add(new Label("cust_id", customer.getId().toString()));
				@SuppressWarnings("serial")
				AjaxLink firstNameLink = new AjaxLink("view") {
					
					@Override
					public void onClick(AjaxRequestTarget target) {
						customerWindow.setContent(new CustomerPanel(customerWindow.getContentId(), 
								customerWindow, customer, OperationType.READ));
						customerWindow.show(target);
					}
				};
				firstNameLink.add(new Label("first_name", customer.getFirstName()));
				item.add(firstNameLink);
				
				item.add(new Label("last_name", customer.getLastName()));
				
				
				@SuppressWarnings("serial")
				AjaxLink editLink = new AjaxLink("edit") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						customerWindow.setContent(new CustomerPanel(customerWindow.getContentId(), 
								customerWindow, customer, OperationType.UPDATE));
						customerWindow.show(target);
					}
				};
				item.add(editLink);
				
				
				@SuppressWarnings("serial")
				AjaxLink deleteLink = new AjaxLink("delete") {

					@Override
					public void onClick(AjaxRequestTarget target) {
						customerWindow.setContent(new CustomerPanel(customerWindow.getContentId(), 
								customerWindow, customer, OperationType.DELETE));
						customerWindow.show(target);
						
					}
				};
				item.add(deleteLink);
				
				
			}
		};
		dataView.setItemsPerPage(MAX_ITEMS_PER_PAGE);
		add(dataView);
		 //add list to container so we can update it
        container.add(dataView);
        
        //add whole thing to page
        add(container);
		
        //pager
        AjaxPagingNavigator navigator = new AjaxPagingNavigator("navigator", dataView);
        navigator.setOutputMarkupPlaceholderTag(true);
        add(navigator);
        
        if(provider.size() <= MAX_ITEMS_PER_PAGE) {
        	navigator.setVisible(false);
        }
        
        @SuppressWarnings("serial")
		AjaxLink deleteLink = new AjaxLink("create") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				customerWindow.setContent(new CustomerPanel(customerWindow.getContentId(), 
						customerWindow, new Customer(), OperationType.CREATE));
				customerWindow.show(target);
			}
        };
        add(deleteLink);
        
        
        customerWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				setResponsePage(new CustomerList(params));
				
			}
		});
        add(customerWindow);
        
		
	}
	
}
